import {
  MaintenancePlanType,
  AddOrUpdateMaintenancePlanQueryType,
} from "../service/maintenancePlan";

// Mock数据
const mockMaintenancePlanList: MaintenancePlanType[] = [
  {
    id: "1",
    planName: "设备A例行检查",
    planDate: 1716163200000,
    deviceId: "1",
  },
  {
    id: "2",
    planName: "设备B保养维护",
    planDate: 1716163200000,
    deviceId: "1",
  },
  {
    id: "3",
    planName: "系统安全巡检",
    planDate: 1716163200000,
    deviceId: "1",
  },
  {
    id: "4",
    planName: "网络设备清洁",
    planDate: 1716163200000,
    deviceId: "1",
  },
];

// 生成唯一ID
const generateId = (): string => {
  return Date.now().toString() + Math.random().toString(36).substr(2, 9);
};

// 模拟延迟
const delay = (ms = 300): Promise<void> => {
  return new Promise((resolve) => setTimeout(resolve, ms));
};

// 获取维护计划列表的mock
export const mockGetMaintenancePlanList = async (): Promise<{
  status: number;
  data: MaintenancePlanType[];
  message: string;
}> => {
  await delay();

  return {
    status: 0,
    data: mockMaintenancePlanList,
    message: "获取维护计划列表成功",
  };
};

// 添加或更新维护计划的mock
export const mockAddOrUpdateMaintenancePlan = async (
  params: AddOrUpdateMaintenancePlanQueryType
): Promise<{
  status: number;
  data: MaintenancePlanType | null;
  message: string;
}> => {
  await delay();

  try {
    // 编辑维护计划
    if (params.id) {
      const index = mockMaintenancePlanList.findIndex(
        (plan) => plan.id === params.id
      );

      if (index === -1) {
        return {
          status: 1,
          data: null,
          message: "维护计划不存在",
        };
      }

      // 更新维护计划信息
      const updatedPlan: MaintenancePlanType = {
        ...mockMaintenancePlanList[index],
        ...Object.fromEntries(
          Object.entries(params).filter(
            ([_, value]) => value !== null && value !== undefined
          )
        ),
      } as MaintenancePlanType;

      mockMaintenancePlanList[index] = updatedPlan;

      return {
        status: 0,
        data: updatedPlan,
        message: "更新维护计划成功",
      };
    }
    // 添加新维护计划
    else {
      // 验证必填字段
      if (!params.planName || !params.planDate) {
        return {
          status: 1,
          data: null,
          message: "计划内容和日期为必填项",
        };
      }

      // 检查该日期是否已有计划
      const existingPlan = mockMaintenancePlanList.find(
        (plan) => plan.planDate === params.planDate
      );
      if (existingPlan) {
        return {
          status: 1,
          data: null,
          message: "该日期已有维护计划",
        };
      }

      const newPlan: MaintenancePlanType = {
        id: generateId(),
        planName: params.planName || "",
        planDate: params.planDate || 0,
        deviceId: params.deviceId || "",
      };

      mockMaintenancePlanList.push(newPlan);

      return {
        status: 0,
        data: newPlan,
        message: "添加维护计划成功",
      };
    }
  } catch (error) {
    return {
      status: 1,
      data: null,
      message: "操作失败，请重试",
    };
  }
};

// 删除维护计划的mock
export const mockDeleteMaintenancePlan = async (
  id: string
): Promise<{ status: number; data: null; message: string }> => {
  await delay();

  const index = mockMaintenancePlanList.findIndex((plan) => plan.id === id);

  if (index === -1) {
    return {
      status: 1,
      data: null,
      message: "维护计划不存在",
    };
  }

  mockMaintenancePlanList.splice(index, 1);

  return {
    status: 0,
    data: null,
    message: "删除维护计划成功",
  };
};
